export default {
	namespaced:true,
	state(){
		return {
			modalShow:false,
			modalData:null, //存放弹窗商品数据
			editIdx:-1,  //区分新增、编辑
			cartList:[
				// {
				// 	id:1,
				// 	french: "Napoléon aux myrtilles",
				// 	name: "蓝莓轻乳拿破仑",
				// 	img: "https://static.mcake.com/new_goods/lanmeiqingrunapolun/N0301/list/1.jpg",
				// 	list:[
				// 		{id:1,fittings: "5套餐具", bcid: 1, price: "198.00", size: "16cm*10cm*5cm", sku: "n0301", bcname: "蛋糕"},
				// 		{id:2,fittings: "10套餐具", bcid: 1, price: "298.00", size: "23cm*14cm*5cm", sku: "n0302", bcname: "蛋糕"},
				// 		{id:3,fittings: "15套餐具", bcid: 1, price: "428.00", size: "28cm*18cm*5cm", sku: "n0303", bcname: "蛋糕"},
				// 		{id:4,fittings: "20套餐具", bcid: 1, price: "728.00", size: "36cm*22cm*5.5cm", sku: "n0305", bcname: "蛋糕"}
				// 	],
				// 	dropIdx:0,  //记录选中的子商品序号
				// 	num:1
				// },
				// {
				// 	french: "Napoléon aux myrtilles",
				// 	name: "蓝莓轻乳拿破仑",
				// 	img: "https://static.mcake.com/new_goods/lanmeiqingrunapolun/N0301/list/1.jpg",
				// 	list:[
				// 		{fittings: "5套餐具", bcid: 1, price: "198.00", size: "16cm*10cm*5cm", sku: "n0301", bcname: "蛋糕"},
				// 		{fittings: "10套餐具", bcid: 1, price: "298.00", size: "23cm*14cm*5cm", sku: "n0302", bcname: "蛋糕"},
				// 		{fittings: "15套餐具", bcid: 1, price: "428.00", size: "28cm*18cm*5cm", sku: "n0303", bcname: "蛋糕"},
				// 		{fittings: "20套餐具", bcid: 1, price: "728.00", size: "36cm*22cm*5.5cm", sku: "n0305", bcname: "蛋糕"}
				// 	],
				// 	dropIdx:1,  //记录选中的子商品序号
				// 	num:2
				// }
			]
		}
	},
	getters:{
		allInfo(state){  //购物车统计信息
			let allCheck = state.cartList.every((item)=>{
				return item.ischeck
			})
			let checkPrice=0 //勾选总价
			let allNum=state.cartList.length  //购物车商品总数
			state.cartList.forEach(item=>{
				if(item.ischeck){
					let {list,dropIdx,num} = item
					checkPrice += list[dropIdx].price * num
				}
			})
			return {
				allCheck,  //全选状态
				checkPrice,
				allNum
			}
		}
	},
	mutations:{
		modalMut(state,bool){  //控制弹窗显示
			state.modalShow = bool
		},
		modalDataMut(state,{goodObj,editIdx}){ //控制弹窗数据
			state.modalData = goodObj
			state.editIdx = editIdx
		},
		cartInitMut(state,cartList){
			state.cartList = cartList
		},
		cartAddMut(state,goodObj){ //加入购物车、编辑购物车
			console.log(state.editIdx);
			let {id,dropIdx,num} = goodObj
			let {cartList,editIdx} = state
			let len = cartList.length
			if(editIdx!=-1){ //【1】编辑购物车
				cartList[editIdx] = goodObj
				state.cartList = [...cartList]
				return
			}
			for(let i=0;i<len;i++){
				if(cartList[i].id==id&&cartList[i].dropIdx==dropIdx){ //【2】同类商品改数量
					state.cartList[i].num+=num
					return
				}
			}
			goodObj.ischeck = true  //标志购物车勾选状态
			state.cartList.push(goodObj) //【3】新增新商品
		},
		cartCheckMut(state,idx){ //单选 
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
		},
		cartAllCheckMut(state,allCheck){ //全选，allCheck是原来的全选状态
			state.cartList.forEach(item=>{
				item.ischeck = !allCheck
			})
		}
	}
}